//
//  LEETabController.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/12.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

class LEETabController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let leeTabBar = LEETabBar()
        
        self.setValue(leeTabBar, forKey: "tabBar")
        
        addChildViewControllers()
        
        //中间按钮点击闭包回调
        leeTabBar.btnClick = {
        
            print("中间按钮被点击了")
            let V = LEEBackgroundView()
            
            V.show(fromVc: self)
        }
        
    }
    
    private func addChildViewControllers() {
    
        addChildVC(vc: LEEHomeTableController(), title: "首页", imageName: "tabbar_home", index: 1)
        addChildVC(vc: LEEMessageController(), title: "消息", imageName: "tabbar_message_center", index: 2)
        addChildVC(vc: LEEDiscoverController(), title: "发现", imageName: "tabbar_discover", index: 3)
        addChildVC(vc: LEEProfileController(), title: "我的", imageName: "tabbar_profile", index: 4)
    
    }
    
    
    private func addChildVC(vc : UIViewController, title : String, imageName : String, index : Int) {
        
        vc.title = title
        
        vc.tabBarItem.tag = index
        //设置文字颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .selected)
        //设置文字大小(只能在默认状态下设置)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: .normal)
        
        vc.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        let nav = LEENavController(rootViewController: vc)
        
        addChildViewController(nav)
        
    }

   
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index : Int = 1
        
        for itemBar in tabBar.subviews {
            
            if itemBar.isMember(of: NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag {
                    //找到点击的这个item
                    for v in itemBar.subviews {
                        
                        if v.isMember(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            
                            UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 8, options: UIViewAnimationOptions.curveEaseIn, animations: {
                                
                                v.transform = CGAffineTransform.identity
                                
                            }, completion: { (_) in
                                
                            })
                            
                        }
                        
                    }
                    
                }
                index += 1
            }
            
        }
    
    }

}
